home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_contains.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.6 KB  |  172 lines

  1. from test_support import TestFailed, have_unicode
  2.  
  3. class base_set:
  4.  
  5.     def __init__(self, el):
  6.         self.el = el
  7.  
  8. class set(base_set):
  9.  
  10.     def __contains__(self, el):
  11.         return self.el == el
  12.  
  13. class seq(base_set):
  14.  
  15.     def __getitem__(self, n):
  16.         return [self.el][n]
  17.  
  18. def check(ok, *args):
  19.     if not ok:
  20.         raise TestFailed, " ".join(map(str, args))
  21.  
  22. a = base_set(1)
  23. b = set(1)
  24. c = seq(1)
  25.  
  26. check(1 in b, "1 not in set(1)")
  27. check(0 not in b, "0 in set(1)")
  28. check(1 in c, "1 not in seq(1)")
  29. check(0 not in c, "0 in seq(1)")
  30.  
  31. try:
  32.     1 in a
  33.     check(0, "in base_set did not raise error")
  34. except TypeError:
  35.     pass
  36.  
  37. try:
  38.     1 not in a
  39.     check(0, "not in base_set did not raise error")
  40. except TypeError:
  41.     pass
  42.  
  43. # Test char in string
  44.  
  45. check('c' in 'abc', "'c' not in 'abc'")
  46. check('d' not in 'abc', "'d' in 'abc'")
  47.  
  48. try:
  49.     '' in 'abc'
  50.     check(0, "'' in 'abc' did not raise error")
  51. except TypeError:
  52.     pass
  53.  
  54. try:
  55.     'ab' in 'abc'
  56.     check(0, "'ab' in 'abc' did not raise error")
  57. except TypeError:
  58.     pass
  59.  
  60. try:
  61.     None in 'abc'
  62.     check(0, "None in 'abc' did not raise error")
  63. except TypeError:
  64.     pass
  65.  
  66.  
  67. if have_unicode:
  68.  
  69.     # Test char in Unicode
  70.  
  71.     check('c' in unicode('abc'), "'c' not in u'abc'")
  72.     check('d' not in unicode('abc'), "'d' in u'abc'")
  73.  
  74.     try:
  75.         '' in unicode('abc')
  76.         check(0, "'' in u'abc' did not raise error")
  77.     except TypeError:
  78.         pass
  79.  
  80.     try:
  81.         'ab' in unicode('abc')
  82.         check(0, "'ab' in u'abc' did not raise error")
  83.     except TypeError:
  84.         pass
  85.  
  86.     try:
  87.         None in unicode('abc')
  88.         check(0, "None in u'abc' did not raise error")
  89.     except TypeError:
  90.         pass
  91.  
  92.     # Test Unicode char in Unicode
  93.  
  94.     check(unicode('c') in unicode('abc'), "u'c' not in u'abc'")
  95.     check(unicode('d') not in unicode('abc'), "u'd' in u'abc'")
  96.  
  97.     try:
  98.         unicode('') in unicode('abc')
  99.         check(0, "u'' in u'abc' did not raise error")
  100.     except TypeError:
  101.         pass
  102.  
  103.     try:
  104.         unicode('ab') in unicode('abc')
  105.         check(0, "u'ab' in u'abc' did not raise error")
  106.     except TypeError:
  107.         pass
  108.  
  109.     # Test Unicode char in string
  110.  
  111.     check(unicode('c') in 'abc', "u'c' not in 'abc'")
  112.     check(unicode('d') not in 'abc', "u'd' in 'abc'")
  113.  
  114.     try:
  115.         unicode('') in 'abc'
  116.         check(0, "u'' in 'abc' did not raise error")
  117.     except TypeError:
  118.         pass
  119.  
  120.     try:
  121.         unicode('ab') in 'abc'
  122.         check(0, "u'ab' in 'abc' did not raise error")
  123.     except TypeError:
  124.         pass
  125.  
  126. # A collection of tests on builtin sequence types
  127. a = range(10)
  128. for i in a:
  129.     check(i in a, "%s not in %s" % (`i`, `a`))
  130. check(16 not in a, "16 not in %s" % `a`)
  131. check(a not in a, "%s not in %s" % (`a`, `a`))
  132.  
  133. a = tuple(a)
  134. for i in a:
  135.     check(i in a, "%s not in %s" % (`i`, `a`))
  136. check(16 not in a, "16 not in %s" % `a`)
  137. check(a not in a, "%s not in %s" % (`a`, `a`))
  138.  
  139. class Deviant1:
  140.     """Behaves strangely when compared
  141.  
  142.     This class is designed to make sure that the contains code
  143.     works when the list is modified during the check.
  144.     """
  145.  
  146.     aList = range(15)
  147.  
  148.     def __cmp__(self, other):
  149.         if other == 12:
  150.             self.aList.remove(12)
  151.             self.aList.remove(13)
  152.             self.aList.remove(14)
  153.         return 1
  154.  
  155. check(Deviant1() not in Deviant1.aList, "Deviant1 failed")
  156.  
  157. class Deviant2:
  158.     """Behaves strangely when compared
  159.  
  160.     This class raises an exception during comparison.  That in
  161.     turn causes the comparison to fail with a TypeError.
  162.     """
  163.  
  164.     def __cmp__(self, other):
  165.         if other == 4:
  166.             raise RuntimeError, "gotcha"
  167.  
  168. try:
  169.     check(Deviant2() not in a, "oops")
  170. except TypeError:
  171.     pass
  172.